<?php
namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

//引入数据库
use think\Db;

class Analysis extends Backend {
    public function __construct() {
        parent::__construct();

        $this->CateModel = model( 'Post.Cate' );
        $this->PostModel = model( 'Post.Post' );
        $this->UserModel = model( 'User.User' );

    }

    public function post() {
        // 获取分类表的id和name
        $catelist = $this->CateModel->field( 'id,name' )->order( 'id DESC' )->select();

        // array_column() 抽取出二维数组中指定的字段变成一个新的数组
        $row = array_column( $catelist, 'name' );

        foreach ( $catelist as $item ) {
            // 已结
            $where = [
                'accept' => [ 'exp', Db::raw( 'is not null' ) ],
                'cateid' => $item[ 'id' ],
            ];
            $AcceptList[] = $this->PostModel->where( $where )->count();

            // 未结
            $whres = [
                'accept' => [ 'exp', Db::raw( 'is null' ) ],
                'cateid' => $item[ 'id' ]
            ];
            $NotAcceptList[] = $this->PostModel->where( $whres )->count();
        }

        $this->assign( [
            'catelist' => json_encode( $row ),
            'AcceptList' => json_encode( $AcceptList ),
            'NotAcceptList' => json_encode( $NotAcceptList ),
        ] );

        return $this->fetch();
    }

    // 用户量统计

    public function user() {
        // 全部用户
        $userList = $this->UserModel->order( 'createtime', 'dsec' )->select();
        $userAll = count( $userList );

        // 认证用户
        $authList = $this->UserModel->where( [ 'auth'=>'1' ] )->select();
        $authUser = count( $authList );

        // 未认证用户
        $noAuthUser = $this->UserModel->where( [ 'auth'=>'0' ] )->count();

        // 全部用户数量
        $timeUser = [];
        foreach ( $userList as $item ) {
            $timeUser[] = explode( ' ', $item[ 'createtime' ] )[ 0 ];
        }
        $timeUser = array_count_values( $timeUser );

        // 认证用户数量
        $timeAuth = [];
        foreach ( $authList as $item ) {
            $timeAuth[] = explode( ' ', $item[ 'createtime' ] )[ 0 ];
        }
        $timeAuth = array_count_values( $timeAuth );

        $this->assign( [
            'userAll' => $userAll,
            'authUser' => $authUser,
            'noAuthUser' => $noAuthUser,
            'timeUser' => json_encode( $timeUser ),
            'timeAuth' => json_encode( $timeAuth )
        ] );

        return $this->fetch();
    }
}

?>